package com.robusta.cityuate.services;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import com.robusta.cityuate.R;
import com.robusta.cityuate.activities.ParkingStatusActivity;
import com.robusta.cityuate.objects.Reservation;

public class ScheduledNotifications extends BroadcastReceiver {

	public static final int CHECK_OUT_NOTI_ID=1;
	public static final int SAFE_TIME=600000;
	public static final int NOTI_REQUEST_CODE=1234;
	
	
	@Override
	public void onReceive(Context context, Intent intent) {
		Reservation notifiedReservation= (Reservation)intent.getExtras().getSerializable(Reservation.RESERVATION);
		Reservation savedReservation=Reservation.restoreReservation(context);
		if (savedReservation==null || savedReservation.getId()!=notifiedReservation.getId()) 
			return;
		// notification is selected
	    Intent toOpenIntent = new Intent(context, ParkingStatusActivity.class);
	    PendingIntent pIntent = PendingIntent.getActivity(context, 0, toOpenIntent, 0);

	    // Build notification
	    // Actions are just fake
	    Notification noti = new NotificationCompat.Builder(context)
	        .setContentTitle(context.getString(R.string.app_name))
	        .setContentText(context.getString(R.string.check_out_before)).setSmallIcon(R.drawable.ic_launcher)
	        .setContentIntent(pIntent).build();
	    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
	    // Hide the notification after its selected
	    noti.flags |= Notification.FLAG_AUTO_CANCEL;
	    noti.defaults |= Notification.DEFAULT_SOUND;
	    notificationManager.notify(CHECK_OUT_NOTI_ID, noti);
	}
	
	
	/**
	 * Schedule notification on specific time
	 * @param context
	 * @param time
	 */
	public static void scheduleNotification(Context context, Reservation reservation) {
		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		
		
		// Prepare the intent which should be launched at the date
		Intent intent = new Intent(context, ScheduledNotifications.class);
		intent.putExtra(Reservation.RESERVATION, reservation);
		// Prepare the pending intent
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				NOTI_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		// Register the alert in the system. You have the option to define if
		// the device has to wake up on the alert or not
		alarmManager.set(AlarmManager.RTC_WAKEUP, reservation.getCheckoutTime().getTime()-SAFE_TIME,pendingIntent);
	}
	/**
	 * Cancel sceduled task
	 * @param context
	 * @param reservation
	 */
	public static void cancelScheduledNotification(Context context, Reservation reservation){
		AlarmManager alarmManager = (AlarmManager) context
				.getSystemService(Context.ALARM_SERVICE);
		
		Intent intent = new Intent(context, ScheduledNotifications.class);
		intent.putExtra(Reservation.RESERVATION, reservation);
		// Prepare the pending intent
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
				NOTI_REQUEST_CODE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
		alarmManager.cancel(pendingIntent);
		
	}

}
